package org.example;
import java.util.*;
/**
 * 加减法口算练习系统-v0.3
 * 支持无重复题目，可配置加减法比例
 */
public class CalculationSystemV03 {
    static class Question {
        String expression;
        int answer;
        String key; // 用于判断重复的关键字
        Question(String expression, int answer, String key) {
            this.expression = expression;
            this.answer = answer;
            this.key = key;
        }
    }
    public static void main(String[] args) {
        System.out.println("加减法口算练习系统-v0.3");
        System.out.println("========================");
        Random random = new Random();
        int totalQuestions = 50;
        int additionRatio = 60; // 加法占60%
        Set<String> generatedKeys = new HashSet<>();
        List<Question> questions = new ArrayList<>();
        // 计算需要的加减法题目数量
        int additionCount = (int) (totalQuestions * additionRatio / 100.0);
        int subtractionCount = totalQuestions - additionCount;
        // 生成加法题目（无重复）
        while (questions.size() < additionCount) {
            int num2 = random.nextInt(100);
            int num1 = random.nextInt(100 - num2 + 1);
            // 生成唯一标识（考虑交换律，a+b和b+a视为相同）
            String key = num1 < num2 ? (num1 + "+" + num2) : (num2 + "+" + num1);
            if (!generatedKeys.contains(key)) {
                generatedKeys.add(key);
                String expression = num1 + " + " + num2 + " = ";
                questions.add(new Question(expression, num1 + num2, key));
            }
        }
        // 生成减法题目（无重复）
        while (questions.size() < totalQuestions) {
            int num1 = random.nextInt(100);
            int num2 = random.nextInt(num1 + 1);
            // 减法的唯一标识
            String key = num1 + "-" + num2;
            if (!generatedKeys.contains(key)) {
                generatedKeys.add(key);
                String expression = num1 + " - " + num2 + " = ";
                questions.add(new Question(expression, num1 - num2, key));
            }
        }
        // 打乱题目顺序
        Collections.shuffle(questions);
        // 显示题目
        for (int i = 0; i < questions.size(); i++) {
            System.out.printf("%-20s", questions.get(i).expression);
            if ((i + 1) % 4 == 0) {
                System.out.println();
            }
        }
        // 显示答案
        System.out.println("\n\n答案：");
        System.out.println("=====");
        for (int i = 0; i < questions.size(); i++) {
            System.out.printf("%-20s%d", questions.get(i).expression, questions.get(i).answer);
            if ((i + 1) % 2 == 0) {
                System.out.println();
            }
        }
        System.out.println("\n统计信息：");
        System.out.println("加法题目：" + additionCount + "道");
        System.out.println("减法题目：" + subtractionCount + "道");
        System.out.println("========================");
    }
}